<%--
  Created by IntelliJ IDEA.
  User: yasser
  Date: 2018/12/12
  Time: 21:43
  To change this template use File | Settings | File Templates.
--%>
<%@page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>
<%@ page import="service.CommodityLogic" %>
<%@ page import="entity.Commodity" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
    <base href="<%=basePath%>">
    <title>商品页</title>

    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="js/lhgcore.js"></script>
    <script type="text/javascript" src="js/lhgdialog.js"></script>
    <script type="text/javascript">
        function selflog_show(id)
        {
            var num =  document.getElementById("number").value;
            <%--J.dialog.get({id: 'haoyue_creat',title: '购物成功',width: 600,height:400, link: '<%=path%>/controller/CartServlet?id='+id+'&num='+num+'&action=add', cover:true});--%>
            J.dialog.get({id: 'haoyue_creat',title: '购物成功',width: 600,height:400, link: '<%=path%>/ServletCartDemo/CartServlet?id='+id+'&num='+num+'&action=add', cover:true});
        }
        function add()
        {
            var num = parseInt(document.getElementById("number").value);
            if(num<100)
            {
                document.getElementById("number").value = ++num;
            }
        }
        function sub()
        {
            var num = parseInt(document.getElementById("number").value);
            if(num>1)
            {
                document.getElementById("number").value = --num;
            }
        }
    </script>

</head>
<body>
<h1>商品详情</h1>
<a href="index.jsp">首页</a> >> <a href="index.jsp">商品列表</a>
<hr>
<div style="text-align: center">
    <table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <!-- 商品详情 -->
            <%
                CommodityLogic cdlogic = new CommodityLogic();
                Commodity commodity = cdlogic.getCommodityById(Integer.parseInt(request.getParameter("id")));
                if(commodity!=null)
                {
            %>
            <td width="70%" valign="top">
                <table>
                    <tr>
                        <td rowspan="5"><img src="images/<%=commodity.getImg()%>" width="200" height="160"/></td>
                    </tr>
                    <tr>
                        <td><B><%=commodity.getName() %></B></td>
                    </tr>
                    <tr>
                        <td>产地：<%=commodity.getAddress()%></td>
                    </tr>
                    <tr>
                        <td>价格：<%=commodity.getPrice() %>￥</td>
                    </tr>
                    <tr>
                        <td>购买数量：<span id="sub" onclick="sub();">-</span><input type="text" id="number" name="number" value="1" size="2"/><span id="add" onclick="add();">+</span></td>
                    </tr>
                </table>
                <div id="cart">
                    <img src="images/buy_now.png"><a href="javascript:selflog_show(<%=commodity.getId()%>)"><img src="images/in_cart.png"></a><a href="ServletCartDemo/CartServlet?action=show"><img src="images/view_cart.jpg"/></a>
                </div>
            </td>
            <%
                }
            %>
            <%
                String list ="";
                //从客户端获得Cookies集合
                Cookie[] cookies = request.getCookies();
                //遍历这个Cookies集合
                if(cookies!=null&&cookies.length>0)
                {
                    for(Cookie c:cookies)
                    {
                        if(c.getName().equals("ListViewCookie"))
                        {
                            list = c.getValue();
                        }
                    }
                }

                list+=request.getParameter("id")+",";
                //如果浏览记录超过1000条，清零.
                String[] arr = list.split(",");
                if(arr!=null&&arr.length>0)
                {
                    if(arr.length>=1000)
                    {
                        list="";
                    }
                }
                Cookie cookie = new Cookie("ListViewCookie",list);
                response.addCookie(cookie);

            %>
            <!-- 浏览过的商品 -->
            <td width="30%" bgcolor="#EEE" align="center">
                <br>
                <b><font color="#FF7F00">您浏览过的商品</font></b><br>
                <!-- 循环开始 -->
                <%
                    ArrayList<Commodity> cdlist = cdlogic.getViewList(list);
                    if(cdlist!=null&&cdlist.size()>0 )
                    {
                        System.out.println("itemlist.size="+cdlist.size());
                        for(Commodity c:cdlist)
                        {

                %>
                <div>
                    <dl>
                        <dt>
                            <a href="details.jsp?id=<%=c.getId()%>"><img src="images/<%=c.getImg() %>" width="120" height="90" border="1"/></a>
                        </dt>
                        <dd class="dd_name"><%=c.getName() %></dd>
                        <dd class="dd_city">产地:<%=c.getAddress() %>&nbsp;&nbsp;价格:<%=c.getPrice() %> ￥ </dd>
                    </dl>
                </div>
                <%
                        }
                    }
                %>
                <!-- 循环结束 -->
            </td>
        </tr>
    </table>
</div>
</body>
</html>
